from datetime import date, datetime
from django.conf import settings
from django.http import HttpResponse
from django.views.decorators.cache import cache_page
from apps.news.models import Record
from core.decorators import render
from core.models import Category

@render("rules.html", is_root_html=True)
def rules(request):
    return {}

def payclick(request):
    return HttpResponse("pay-click.ru")

@render("sitemap.xml", type='xml')
def sitemap(request):

    items = []
    items.append({
        'url': "%s/" % settings.LIVE_ENGINE_URL,
        'date': "%s" % date.today(),
        'priority': '1.0'
    })

    categories = Category.objects.filter(
        parent = None
    )
    for category in categories:
        items.append({
            'url': "%s/%s/" % (settings.LIVE_ENGINE_URL, category.url),
            'date': "%s" % date.today(),
            'priority': '0.8'
        })

        records = Record.objects.filter(
            category = category,
            status = Record.STATUS_PUBLISHED,
            publish_date__lte = datetime.now()
        )

        for record in records:
            items.append({
                'url': "%s/%s/%s-%s.html" % (settings.LIVE_ENGINE_URL, category.url, record.pk, record.url),
                'date': "%s" % record.create_date.strftime("%Y-%m-%d"),
                'priority': '0.6'
            })


    return {
        'items': items
    }
